package br.com.java.beans.converters;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

public class DateConverter implements Converter
{
	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

	public Object getAsObject(FacesContext context, UIComponent component, String text) 
	{
		try 
		{
			if(text.equals(""))
				return null;
			
			Date date = sdf.parse(text);
			return date;
		}
		catch (ParseException e) 
		{
			throw new IllegalArgumentException();
		}
	}

	public String getAsString(FacesContext context, UIComponent component, Object value) 
	{
		Date date = (Date)value;
		
		if (date==null)
			return "";
		else
			return sdf.format(date);
	}

}
